package org.example.cake.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.cake.common.Rep;
import org.example.cake.entity.Address;
import org.example.cake.entity.User;
import org.apache.ibatis.annotations.Param;
import org.example.cake.common.Rep;
import org.example.cake.entity.Address;
import org.example.cake.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;
    /**
     * 分页查询所有地址
     * @return 地址集合
     */
    @GetMapping("/selectAllAddress")
    public Rep selectAllAddress(@RequestParam(required = false)String consignee,
                                @RequestParam(required = false)String phone,
                                @RequestParam(required = false)String province,
                                @RequestParam(required = false)String city,
                                @RequestParam(required = false)String district, Integer pageNum  , Integer pageSize){
        pageNum  = pageNum  == null ? 1 : pageNum ;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNum , pageSize);
        List<Address> addressList = addressService.selectAllAddresss(consignee,phone,province,city,district);
        PageInfo<Address> pageInfo = new PageInfo<>(addressList);
        return new Rep().addData("Address",pageInfo);

    }

    /**
     * 修改地址
     * @return 修改地址
     */
    @PostMapping("/updateAddresss")
    public Rep updateAddresss(@RequestBody Address address) {
        // 1. 执行更新
        int result = addressService.updateByPrimaryKey(address);

        if (result > 0) {
            Address updatedAddress = addressService.getById(address.getAddressid());
            return new Rep()
                    .addData("address", updatedAddress)
                    .addData("msg", "修改地址成功");
        } else {
            // 更新失败时返回错误信息
            return new Rep().addData("msg", "修改地址失败");
        }
    }
    /**
     * 删除地址
     * @return 删除地址
     */
    @GetMapping("/deleteAddress")
    public Rep  deleteAddress(Integer addressid){
        addressService.deleteByPrimaryKey(addressid);
        return new Rep().addData("msg","删除地址成功");
    }







    /**
     * 根据用户id查询收货地址
     * @param userid 用户id
     * @return 地址集合
     */

    @GetMapping("/selectAddressByUserid")
    public Rep selectAddressByUserid(@RequestParam("userid") Integer userid){
        List<Address> selectAddressByUserid = this.addressService.selectAddressByUserid(userid);
        return new Rep().addData("selectAddressByUserid",selectAddressByUserid);
    }
    /**
     * 根据地址id查询地址详情
     * @param addressid 地址id
     * @return 地址实体
     */
    @GetMapping("/selectAddressById")
    public Rep selectAddressById(@Param("addressid") Integer addressid){
        Address addressById = this.addressService.selectAddressById(addressid);
        return new Rep().addData("selectAddressById",addressById);
    }
    /**
     * 修改地址
     * @param address 地址实体
     * @return 收影响的行数
     */
    @PostMapping("/updateAddress")
    public int updateAddress(@RequestBody Address address){
        int updateAddress = this.addressService.updateAddress(address);
        if (updateAddress > 0){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 新增地址
     * @param address 地址对象
     * @return 收影响的行数
     */
    @PostMapping("/insertAddress")
    public int insertAddress(@RequestBody Address address){
        int insertAddress = this.addressService.insertAddress(address);
        if (insertAddress > 0){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 根据地址id删除地址
     * @param addressid 地址id
     * @return 受影响的行数
     */
    @PostMapping("/deleteAddressById")
    public int deleteAddressById(@RequestParam("addressid") Integer addressid){
        int deleteAddress = this.addressService.deleteAddressById(addressid);
        if (deleteAddress > 0){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 修改默认地址
     * @param defaultaddress 默认地址 0：默认 1：不默认
     * @param addressid 地址id
     * @return 受影响的行数
     */
    @PostMapping("/updateDefaultAddress")
    public int updateDefaultAddress(@RequestParam("defaultaddress") Integer defaultaddress,@RequestParam("addressid") Integer addressid){
        int updateDefaultAddress = this.addressService.updateDefaultAddress(defaultaddress, addressid);
        if (updateDefaultAddress > 0){
            return 1;
        }else {
            return 0;
        }
    }
}
